<?php
require_once dirname(__FILE__).'/Controller.php';
if (!Editor_Controller::isAllowEdit()) {
	header('Location: http://www.yahoo.co.jp');
	exit;
}
$js = '';
if (!empty($_REQUEST['filename'])) {
	$htmlFileName = $_REQUEST['filename'];
} else {
	$htmlFileName = Editor_Controller::createFileName();
}
$saveDir = dirname(__FILE__) . '/../contents';
$saveDir = realpath($saveDir);

$fileName = $_SERVER['DOCUMENT_ROOT'].'/contents/'.$htmlFileName;
$fileName = $saveDir.'/'.$htmlFileName;
$unixFileName = str_replace("\\", '/', $fileName);
$url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $unixFileName);
file_put_contents($fileName, Editor_Controller::createHtml());
$js = file_get_contents(dirname(__FILE__).'/modify.js');
$replacer = array(
		'{?MESSAGE?}' => $htmlFileName.'を追加しました。',
		'{?URL?}' => $url,
);
$js = Editor_Controller::fetchMarty($replacer, $js);

echo Editor_Controller::createHtml($js);